<template>
  <div class="container">
    <el-row :gutter="10" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table
      :data="menuList"
      height="400"
      style="width: 100%"
      v-loading="loading"
      row-key="id"
      :header-cell-style="{ backgroundColor: '#f1f1f1', color: '#333' }"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        prop="title"
        label="菜单名称"
        width="160"
        :indent="0"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column prop="icon" label="图标" align="center">
        <!-- <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template> -->
      </el-table-column>
      <el-table-column prop="sort" label="排序" align="center"> </el-table-column>
      <el-table-column prop="component" label="组件路径" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center"> </el-table-column>
      <el-table-column prop="create_time" label="创建时间" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="handleUpdate(scope.row)"
            type="text"
            size="small"
            icon="el-icon-edit"
          >
            修改
          </el-button>
          <el-button
            @click.native.prevent="handleAdd(scope.row)"
            type="text"
            size="small"
            icon="el-icon-plus"
          >
            新增
          </el-button>
          <el-button
            @click.native.prevent="handleDelete(scope.row)"
            type="text"
            size="small"
            icon="el-icon-delete"
          >
            移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级菜单">
              <treeselect
                v-model="form.pid"
                :options="menuOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级菜单"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="菜单类型" prop="type">
              <el-radio-group v-model="form.type">
                <el-radio label="D">目录</el-radio>
                <el-radio label="M">菜单</el-radio>
                <el-radio label="B">按钮</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item v-if="form.type != 'B'" label="菜单图标">
              <el-popover
                placement="bottom-start"
                width="460"
                trigger="click"
                @show="$refs['iconSelect'].reset()"
              >
                <IconSelect ref="iconSelect" @selected="selected" />
                <el-input
                  slot="reference"
                  v-model="form.icon"
                  placeholder="点击选择图标"
                  readonly
                >
                  <svg-icon
                    v-if="form.icon"
                    slot="prefix"
                    :icon-class="form.icon"
                    class="el-input_icon"
                    style="height: 40px; width: 16px"
                  />
                  <i v-else slot="prefix" class="el-icon-search el-input_icon"></i>
                </el-input>
              </el-popover>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入菜单名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="sort">
              <el-input v-model="form.sort" controls-position="right" :min="0"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否外链" prop="is_link">
              <el-radio-group v-model="form.is_link">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.type != 'B'" label="路由地址" prop="path">
              <el-input v-model="form.path" placeholder="请输入路由地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.type == 'M'">
            <el-form-item label="组件路径" prop="component">
              <el-input v-model="form.component" placeholder="请输入组件路径"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.type != 'D'" label="权限标识">
              <el-input
                v-model="form.perms"
                placeholder="请输入权限标识"
                maxlength="50"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.type != 'B'" label="显示状态">
              <el-radio-group v-model="form.visible">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.type != 'B'" label="菜单状态">
              <el-radio-group v-model="form.status">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { menuList, addMenu, editMenu, deleteMenu, getMenu } from "@/api/system/menu";
import Treeselect from "@riophae/vue-treeselect";
import IconSelect from "@/components/IconSelect/index";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Menu",
  components: {
    Treeselect,
    IconSelect,
  },
  data() {
    return {
      title: "",
      menuList: [],
      menuOptions: [],
      loading: false,
      open: false,
      form: {},
      rules: {
        name: [{ require: true, message: "菜单名称不能为空", trigger: "blur" }],
        sort: [{ require: true, message: "菜单顺序不能为空", trigger: "blur" }],
        path: [{ require: true, message: "路由地址不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getMenuList();
  },
  methods: {
    selected(name) {
      this.form.icon = name;
    },
    getMenuList() {
      this.loading = true;
      menuList()
        .then((res) => {
          if (res.code === 200) {
            const result = res.data;
            this.menuList = this.handleTree(result, "id", "pid");
            this.loading = false;
          }
        })
        .catch((err) => console.log(err));
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.title,
        children: node.children,
      };
    },
    getTreeselect() {
      menuList().then((res) => {
        this.menuOptions = [];
        const menu = { id: "0", title: "主类目", children: [] };
        menu.children = this.handleTree(res.data, "id", "pid");
        this.menuOptions.push(menu);
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: undefined,
        pid: "0",
        title: undefined,
        icon: undefined,
        type: "D",
        sort: undefined,
        is_link: "0",
        visible: "1",
        status: "1",
      };

      this.resetForm("form");
    },
    handleAdd(row) {
      console.log(this.form);
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.pid = row.pid;
      } else {
        this.form.pid = "0";
      }
      this.open = true;
      this.title = "添加菜单";
    },
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      getMenu({ id: row.id })
        .then((res) => {
          this.form = res.data;
          this.open = true;
          this.title = "修改菜单";
        })
        .catch((err) => console.log(err));
    },
    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            editMenu(this.form).then((res) => {
              if (res.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getMenuList();
              }
            });
          } else {
            addMenu(this.form)
              .then((res) => {
                if (res.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getMenuList();
                }
              })
              .catch((err) => console.log(err));
          }
        }
      });
    },
    handleDelete(row) {
      this.$confirm("是否确认删除名称为" + row.title + '"的数据项？"', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return deleteMenu({ id: row.id });
        })
        .then((res) => {
          if (res.success) {
            this.getMenuList();
            this.msgSuccess("删除成功");
          } else {
            this.msgError(res.msg);
          }
        });
    },
  },
};
</script>
<style lang="stylus" scoped>
.container
  .header-style
    background #ccc
</style>
<style lang="stylus">
.el-table .cell
  padding-right: 10px
</style>
